var big_img_height = 0;
var big_img_width = 0;
var img_scroll_width = 0;
var img_count = 0;
var tip_height = 0;
var bottom_height = 140;
var scroll_right = 0;
var scroll_left = 0;
var scroll_width = 0;
var second_s = 1000;
var second_f = 500;
var second_vf = 200;

$(document).ready(function(){
	
	init()
})

function init(){
	QRCode();
	
	flashWindow()
    $("#1_img").addClass("this_pic");
	$(".img-s").click(function(){
		temp_src = $(this).find("img").attr("src");
		temp_id = $(this).attr("id");
        data_download = $(this).attr("data-download");
        temp_size = $(this).attr("data-size");
        
        returnPixel(temp_src,temp_size);
        $("#download-img").attr("href",data_download);
		$("#left-center").find("img").attr("src",temp_src)
		$("#left-center").find("img").attr("id",temp_id+"b")
		$("#left-center").find("img").attr("data-download",data_download);
		$(".img-s").removeClass("this_pic")
		$(this).addClass("this_pic")
		retunToThisPic($(this).attr("id"))
	})
	$(window).resize(function(){
		flashWindow()
		retunToThisPic($("#left-center").find("img").attr("id"))
	})
	$("#sm-img").click(function(){
		if($(this).find("span").html()=="隐藏缩略图")
		{
			$("#left-footer").animate({height:0},second_f)
			bottom_height = 40;
			var temp = $(window).height()-50-bottom_height;
			$("#left-top").animate({height:temp},second_f)
//			css("display","none")
			
//			flashWindow()
			$(this).find("span").html("显示缩略图")
		}
		else{
			$("#left-footer").animate({height:100},second_f)
			bottom_height = 160;
			var temp = $(window).height()-50-bottom_height;
			$("#left-top").animate({height:temp},second_f)
			
//			$("#left-footer").css("display","block")
//			bottom_height = 140;
//			flashWindow()
//			console.log($("#left-center").find("img").attr("id"))
			retunToThisPic($("#left-center").find("img").attr("id"))
			$(this).find("span").html("隐藏缩略图")
		}
		
	})
	$("#next-one-s").click(function(){
		var temp;
		if(scroll_right == 0){
			lastImg("当前为最后一张图片");
			return 
		}
		if(scroll_right>scroll_width){
            scroll_right = scroll_right - scroll_width;
            scroll_left = scroll_left + scroll_width;
			temp = parseInt($("#img-scroll").css("left").slice(0,-2))-scroll_width
			$("#img-scroll").animate({left:temp},second_s)
		}else{
			scroll_left = scroll_left + scroll_right;
			temp = parseInt($("#img-scroll").css("left").slice(0,-2))-scroll_right
			scroll_right = 0;
			$("#img-scroll").animate({left:temp},second_s)
		}
	})
	$("#last-one-s").click(function(){
		var temp;
		if(scroll_left ==0){
			lastImg("当前为第一张图片")
			return
		}
		if(scroll_left>scroll_width){
			scroll_left = scroll_left - scroll_width;
			scroll_right = scroll_right + scroll_width;
			temp = parseInt($("#img-scroll").css("left").slice(0,-2))+scroll_width;
			$("#img-scroll").animate({left:temp},second_s)
		}else{
			scroll_right = scroll_right + scroll_left;
			temp = parseInt($("#img-scroll").css("left").slice(0,-2))+scroll_left;
			scroll_left = 0
			$("#img-scroll").animate({left:temp},second_s)
		}
	})
	
	$("#next-one").click(function(){
		var temp = $("#left-center").find("img").attr("id")
		var base = parseInt(temp.slice(0,-5))
		temp = base +1+"_img";
		retunToThisPic(temp)
		console.log("点击下一个此时的图片坐标"+base)
		if(base<img_count)
		{
			flashImg(temp)
		}else{
			lastImg("当前为最后一张图片");
		}
	})
	
	$("#last-one").click(function(){
		var temp = $("#left-center").find("img").attr("id")
		var base = parseInt(temp.slice(0,-5))
		temp = base -1+"_img";
		retunToThisPic(temp)
		console.log("点击上一个此时的图片坐标"+base)
		if(base>1)
		{
			flashImg(temp)
		}else{
			lastImg("当前为第一张图片")
		}
		
	})
	
	//ie11以下取消全屏
	if(IETester()!=false&&IETester()!="11.0"){
		$("#fullscreen").hide()
	}
	
	//生成二维码
	$("#send-phone").click(function(){
		$('#QRCode').fadeIn(100)
	})
	
	$("#remove").click(function(){
		$('#QRCode').fadeOut(100)
	})
}

function flashImg(s_img){
	console.log("把缩略图放在大图上"+s_img)
	$(".img-s").removeClass("this_pic");
	$("#"+s_img).addClass("this_pic");
	$("#left-center").find("img").attr("src",$("#"+s_img).find("img").attr("src"))
	$("#left-center").find("img").attr("id",$("#"+s_img).attr("id")+"b")
	$("#left-center").find("img").attr("data-download",$("#"+s_img).attr("data-download"));
	$("#download-img").attr("href",$("#"+s_img).attr("data-download"))
	returnPixel($("#"+s_img).find("img").attr("src"),$("#"+s_img).attr("data-size"))
}
function flashWindow(){
	img_count = $(".img-s").length;
	img_scroll_width = img_count * 110;
	big_img_height = $(window).height()-50-bottom_height;
	big_img_width = $(document.body).width()-280+15;
	
	
	$("#img-scroll").width(img_scroll_width)
	$("#left-top").height(big_img_height)
	$("#left").width(big_img_width)
	$("#right").height($(window).height()-110)
	scroll_right = img_scroll_width -$("#left-footer").width()+parseInt($("#img-scroll").css("left").slice(0,-2))+100;
	scroll_width = $("#left-footer").width()-100-100;
	$("#img-scroll").css("left",-scroll_left+100)
}

function QRCode(){
	$("#code").qrcode({ 
    render: "table", //table方式 
    text: location.href //任意内容 
}); 
	$("#QRCode").width($(document.body).width());
	$("#QRCode").height($(window).height());
}
function retunToThisPic(pic_id){
	console.log("滚动条滚动到当前大图的缩略图上"+pic_id)
	var this_pic = parseInt(pic_id.slice(0,-4))
	var temp = this_pic * 110;
	if(temp<(scroll_width/2)){
		scroll_left = 0;
		$("#img-scroll").animate({left:-scroll_left+100},second_vf,function(){
			scroll_right = img_scroll_width -$("#left-footer").width()+parseInt($("#img-scroll").css("left").slice(0,-2))+100;
		})
	}else if(temp>img_scroll_width-scroll_width/2){
		scroll_left =img_scroll_width-$("#left-footer").width()+100;
		scroll_right = 0;
		$("#img-scroll").animate({left:-scroll_left},second_vf)
	}else{
		scroll_left = temp -scroll_width/2
		$("#img-scroll").animate({left:-scroll_left+100},second_f,function(){
			scroll_right = img_scroll_width -$("#left-footer").width()+parseInt($("#img-scroll").css("left").slice(0,-2))+100;
		})
		
		
	}
	
}

function lastImg(tip){
	
	$("#tip").html(tip)
			$("#tip").fadeIn(1000);
			setTimeout(function(){
				$("#tip").fadeOut(1000);
			},3000)
}

function returnPixel(src,size){
	var img = new Image();
	img.src = src;
	$("#img_size").html(img.width+"×"+img.height)
	$("#img_weight").html(size)
	console.log(img.fileSize)
}

